package com.example.home.hospital;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.home.R;
import com.example.home.hospital.activity.HospitalMain;
import com.example.home.hospital.adapter.NormalAdapter;
import com.example.home.hospital.bean.AllCardBean;
import com.example.home.hospital.bean.NormalBean;
import com.example.home.hospital.bean.RegistrationBean;
import com.example.home.tool.OKHttpTool;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;


public class NormalFragment extends Fragment {


    private RecyclerView rcv_list;
    private RegistrationBean.RowsBean regisId;
    private AllCardBean.RowsBean regisId2;

    // 带参数的构造方法
    public NormalFragment(RegistrationBean.RowsBean regisId, AllCardBean.RowsBean regisId2) {
        this.regisId = regisId;
        this.regisId2 = regisId2;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_normal, container, false);
        rcv_list = view.findViewById(R.id.rcv_list);


        getList();


        return view;
    }


    private void getList() {
        OKHttpTool.doNetRequest("prod-api/api/hospital/category/list?type=1&pageNum=1&pageSize=8",
                OKHttpTool.GET,
                RegistrationBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        RegistrationBean registrationBean = (RegistrationBean) obj;

                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindList(registrationBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                }
        );
    }

    private void bindList(RegistrationBean registrationBean) {
        NormalAdapter normalAdapter = new NormalAdapter(getActivity(), registrationBean.getRows(), new NormalAdapter.CallBack() {
            @Override
            public void callback(int position) {
                JSONObject jsonObject = new JSONObject();
                Integer id = regisId.getId();
                Double money = regisId.getMoney();
                String name = regisId2.getName();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
                long time = System.currentTimeMillis();
                String formattedDate = sdf.format(time);
                String type = regisId.getType();
                try {
                    jsonObject.put("categoryId", id);
                    jsonObject.put("money", money);
                    jsonObject.put("patientName", name);
                    jsonObject.put("reserveTime", formattedDate);
                    jsonObject.put("type", type);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                OKHttpTool.doNetRequest("prod-api/api/hospital",
                        OKHttpTool.POST,
                        NormalBean.class,
                        new OKHttpTool.NetCallback() {
                            @Override
                            public void onSuccess(Object obj) {
                                NormalBean normalBean = (NormalBean) obj;
                                getActivity().runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (normalBean.getCode() == 200) {
                                            Toast.makeText(getContext(), "预约成功", Toast.LENGTH_SHORT).show();

                                            // 创建一个Intent对象
                                            Intent intent = new Intent(getActivity(), HospitalMain.class);

                                            // 启动目标Activity
                                            startActivity(intent);

                                        } else if (normalBean.getCode() == 500) {
                                            Toast.makeText(getContext(), "预约失败", Toast.LENGTH_SHORT).show();
                                        }
                                    }

                                });
                            }

                            @Override
                            public void onFailure(String msg) {
                                Log.e("xhf", "==" + msg);
                            }
                        }, jsonObject.toString()
                );
            }

        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        rcv_list.setAdapter(normalAdapter);
        rcv_list.setLayoutManager(linearLayoutManager);
    }
}